using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class StrengthSlider : MonoBehaviour
{
    public float leftClickCount;
    public float rightClickCount;
    public int fixedFrameCount;

    public float acc;
    public float maxAcc;
  
    public float inputOffset;
    public float inputInterval;
    public float maxLeftClickCount;
    public float maxRightClickCount;
    public Slider strengthSlider;
    public Slider leftSlider;
    public Slider rightSlider;
    //public Image handle;

    float leftMinusRight;

    public Train train;

    [Header("CheckArea")]
    public bool CheckSwitch;
    public float leftBorder;
    public float rightBorder;
    public float checkTimer;
    public Image checkArea;
    float lastCheckAreaTime;
    float enterTime;
    public float checkAreaTimeCD;
    public int health;
    public LifeUI lifeUI;

    public float countDown;
    public bool overTimeCheckSwitch;
    float overTime;

    bool hasCostHp = false;
    private void Start()
    {
        lastCheckAreaTime = 0;
    }
    void Update()
    {
        checkArea.gameObject.SetActive(CheckSwitch);
        if (CheckSwitch)
        {
            CheckArea();
        }

        if (overTimeCheckSwitch)
        {
            OverTimeCheck();
        }

        if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
        {
            if(leftClickCount < 0)
            {
                leftClickCount = 0;
            }
            else if(leftClickCount < maxLeftClickCount)
                leftClickCount+=2;
        }
        if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
        {
            if (rightClickCount < 0)
            {
                rightClickCount = 0;
            }
            else if (rightClickCount < maxRightClickCount)
                rightClickCount+=2;
        }

        if (leftClickCount > 0 || rightClickCount > 0)
        {
            leftMinusRight = leftClickCount - rightClickCount;
            if(leftMinusRight > inputInterval)
            {
                Debug.Log("Left > Right");
            }
            else if( Mathf.Abs(leftMinusRight) <= inputInterval)
            {
                Debug.Log("Left == Right");
            }
            if(leftMinusRight < -inputInterval)
            {
                Debug.Log("Left < Right");
            }
        }

        acc = (float)(leftClickCount + rightClickCount) / maxLeftClickCount / 2 * maxAcc * Time.fixedDeltaTime;
        train.SetAcc(acc);
        leftSlider.value = (leftClickCount / maxLeftClickCount);
        rightSlider.value = (rightClickCount / maxRightClickCount);
    }

    private void FixedUpdate()
    {
        if (fixedFrameCount == 3)
        {
            
            if (leftClickCount > 0)
            {
                leftClickCount -= inputOffset;
                if (leftClickCount < 0)
                    leftClickCount = 0;
            }
                
            if (rightClickCount > 0)
            {
                rightClickCount -= inputOffset;
                if (rightClickCount < 0)
                    rightClickCount = 0;
            }
            
            fixedFrameCount = 0;
        }
        fixedFrameCount++;

        if (leftMinusRight > inputInterval)
        {
            strengthSlider.value -= 0.003f;
            
        }
        else if (Mathf.Abs(leftMinusRight) <= inputInterval)
        {
            if (strengthSlider.value > 0.5f)
            {
                strengthSlider.value -= 0.0035f;

            }
            else if (strengthSlider.value < 0.5f)
            {
                strengthSlider.value += 0.0035f;
            }

            strengthSlider.value = Mathf.Abs(strengthSlider.value - 0.5f) < 0.0035 ? 0.5f : strengthSlider.value;
        }
        if (leftMinusRight < -inputInterval)
        {
            strengthSlider.value += 0.003f;
        }
        
    
    }
    void CheckArea()
    {
        UpdateAreaColor();
        if (2 * strengthSlider.value - 1 < leftBorder || 2 * strengthSlider.value - 1 > rightBorder)
        {
            checkTimer += Time.deltaTime;
            if (checkTimer > countDown)
            {
                checkTimer = countDown;
                if (Time.time - lastCheckAreaTime > checkAreaTimeCD)
                {
                    lastCheckAreaTime = Time.time;
                    if (!hasCostHp)
                    {
                        lifeUI.CostHP();
                        hasCostHp = true;
                    }
                }
            }
        }
        else
        {
            checkTimer -= Time.deltaTime;
            if (checkTimer < 0)
                checkTimer = 0;
        }
    }

    void UpdateAreaColor()
    {
        if (checkTimer <= countDown/3)
        {
            checkArea.color = new Vector4(0.5f, 1, 0, 1) + checkTimer / (countDown / 3) * new Vector4(0.5f, 0, 0, 0);
        }
        else if (checkTimer <= countDown)
        {
            checkArea.color = new Vector4(1f, 1, 0, 1) - (checkTimer - (countDown / 3)) / (countDown * 2 / 3) * new Vector4(0, 1f, 0, 0);
        }
    }

    public void SetArea(float left, float right, float time = -1)
    {
        enterTime = Time.time;
        overTime = time;

        overTimeCheckSwitch = true;
        CheckSwitch = true;
        hasCostHp = false;

        leftBorder = left;
        rightBorder = right;
        checkArea.rectTransform.anchoredPosition = new Vector2((right + left) * 100, 0);
        checkArea.rectTransform.sizeDelta = new Vector2((right - left) * 200, checkArea.rectTransform.sizeDelta.y);
    }

    public void CloseArea()
    {
        overTimeCheckSwitch = false;
        CheckSwitch = false;
        checkArea.color = new Vector4(0.5f, 1, 0, 1);
        checkTimer = 0f;
    }

    void OverTimeCheck()
    {
        if (overTime == -1) return;
        if (enterTime + overTime < Time.time)       //����ʱ�� + Ҫ��ͨ����ʱ��С�ڵ�ǰʱ����ʱ
        {
            if (!hasCostHp)
            {
                lifeUI.CostHP();
                hasCostHp = true;
            }
            overTimeCheckSwitch = false;
        }
    }
}
